package com.sz.wallet_demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sz.wallet_demo.config.CurrContextHolder;
import com.sz.wallet_demo.mapper.BalanceChangeMapper;
import com.sz.wallet_demo.mapper.WalletMapper;
import com.sz.wallet_demo.model.BalanceChange;
import com.sz.wallet_demo.model.Wallet;
import com.sz.wallet_demo.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Ts-SuSu
 * @date 2023/9/2 19:28
 * @Description 金额明细管理
 * 4.查询用户钱包金额变动明细*
 */

@RestController
@RequestMapping("/balance")
@Api(tags = "金额明细管理")
public class BalanceChangeController {

    private WalletMapper walletMapper;
    private BalanceChangeMapper balanceChangeMapper;

    public BalanceChangeController(WalletMapper walletMapper, BalanceChangeMapper balanceChangeMapper) {
        this.walletMapper = walletMapper;
        this.balanceChangeMapper = balanceChangeMapper;

    }

    @ApiOperation("金额明细管理-查询用户钱包金额变动明细")
    @GetMapping("/list")
    public Result<List<BalanceChange>> getCurrUserBalanceChangeList() {
        //获取当前用户id
        Integer userId = CurrContextHolder.getNotNullCurr().getUser().getId();

        //查询列表
        List<BalanceChange> list = walletMapper.selectList(new LambdaQueryWrapper<Wallet>()
                        .eq(Wallet::getUserId, userId))
                .stream()
                .map(Wallet::getId)
                .map(id -> balanceChangeMapper.selectList(new LambdaQueryWrapper<BalanceChange>()
                        .eq(BalanceChange::getWalletId, id)))
                .flatMap(Collection::stream)
                .collect(Collectors.toList());
        return Result.success(list, "查询成功");
    }
}
